package com.example.demo.repository;

import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * @Description
 * @Author Ninja
 * @Date 2020/9/22
 **/

@Repository
public class CityClassRepository {

    @PersistenceContext
    private EntityManager entityManager;

    public List entityManagerTest(String countryId){

        String sql = "select * from country c1  LEFT JOIN city c2 ON c2.country_country_id = c1.country_id where c1.country_id = " + countryId;
        Query query = entityManager.createNativeQuery(sql);
        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List list = query.getResultList();
        return list;
    }
}
